package com.zm.spring.jdbc;

import com.zm.spring.jdbc.Bean.Account;
import com.zm.spring.jdbc.Bean.OpRecord;
import com.zm.spring.jdbc.dao.OpRecordDao;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/record/*")
public class QueryRecord extends BasicServlet{

    public void queryRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext cxt = (ApplicationContext) servletContext.getAttribute("cxt");
        OpRecordDao dao = cxt.getBean(OpRecordDao.class);
        Account userInfo = (Account) req.getSession().getAttribute("userInfo");
        System.out.println("userInfo = " + userInfo);
        int aid = userInfo.getId();
        int page =Integer.parseInt(req.getParameter("page"));
        System.out.println("aid = " + aid);
        List<Map<String, Object>> opRecords = dao.selectByAid(aid,page);
        resp.setContentType("application/json;charset=utf-8");
        this.send(resp,opRecords);
    }
}
